avatar = {};
avatar.param = {};
avatar.param.Crop = {};
avatar.cut = false;
avatar.submit = false;
var jcrop_api, boundx, boundy;
avatar.upload = function(){
	
	var times = new Date();
	times = times.getTime();
	jQuery('#headfile').hide().next().show();
	jQuery.ajaxFileUpload({
			url:'album/AlbumPics/ajaxheader',
			secureuri:false,
			fileElementId:'headfile',
			dataType: 'json',
			success: function (data, status){
				var showtip =false;
				if(data.state){
					jQuery('#target').attr('src',CART.url+'../img/'+data.msg+'?_'+times);
				}else{
					//GM.tip(data.msg,false);
					alert(data.msg);
					showtip =true;
				}
				jQuery('#headfile').next().hide().prev().show();
			},
			error: function (data, status, e){
				//GM.tip(e,false);
				jQuery('#headfile').next().hide().prev().show();
			}
		}
	);
};

avatar.resizeTarget = function(info){
	var SW = info[0];
	var SH = info[1];
	var maxW = 400,minW = 150,maxH = 294,minH = 150,showW = 0,showH = 0;
	 
	if(SW <= maxW){
		showW = SW;
		showH = showW*SH/SW;
	}else{
		showW = maxW;
		showH = showW*SH/SW;
	}
	avatar.param.Crop.zoom = SW/showW;
	jQuery('#target').width(showW).height(showH);
};
      // Create variables (in this scope) to hold the API and image size
avatar.createCut = function(){
	if(avatar.cut){
		jcrop_api.destroy();
		return false;
	}
      jQuery('#target').load(function(){
		  avatar.cut = true;
		  avatar.submit = false;
	      $('#target').Jcrop({
	        onChange: updatePreview,
	        onSelect: updatePreview,
	        aspectRatio: 1
	      },function(){
	        // Use the API to get the real image size
	        var bounds = this.getBounds();
	        boundx = bounds[0];
	        boundy = bounds[1];
	        // Store the API in the jcrop_api variable
	        jcrop_api = this;
	        jcrop_api.animateTo([0,0,150,150]);
	      });
      });
};

      function updatePreview(c){
        if (parseInt(c.w) > 0){
          var rx = 150 / c.w;
          var ry = 150 / c.h;

          $('#preview').css({
            width: Math.round(rx * boundx) + 'px',
            height: Math.round(ry * boundy) + 'px',
            marginLeft: '-' + Math.round(rx * c.x) + 'px',
            marginTop: '-' + Math.round(ry * c.y) + 'px'
          });
          avatar.param.Crop.x = c.x;
          avatar.param.Crop.y = c.y;
          avatar.param.Crop.w = c.w;
          avatar.param.Crop.h = c.h;
          avatar.submit = true;
        }
      };

   avatar.submitHead = function(){
	 if(avatar.submit){
		 jQuery('#s_load').show().prev().hide();
		 var times = new Date();
		 times = times.getTime();
		 jQuery.ajax({
				type: "POST",
				url: GM.RootScript+'/album/AlbumPics/ajaxheadercut/'+avatar.param.pid,
				dataType:"json",
				data:avatar.param,
				success: function(data){
			 		if(data.state){
			 			jQuery('#avatar_header').attr('src',GM.RootScript+'/img/'+data.msg+'?_'+times);
			 			jQuery('#avatar_header_right').attr('src',GM.RootScript+'/img/'+data.msg+'?_'+times);
			 			jQuery('.right_user_tx #avatar_header').attr('src',GM.RootScript+'/img/'+data.msg+'?_'+times);
			 		}else{
			 			
			 		}
			    },
		 		error: function (data, status, e)
				{
			    	 jQuery('#s_load').hide().prev().show();
				}
			});
	 }else{
		 GM.pop(jQuery('#headfile'),'请选择一张图片',false,25,0);
	 }
   };